package com.edu.dao;

import com.edu.entity.Question;
import com.edu.entity.QuestionOptions;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface QuestionMapper {
    /*
    * {1}查询题库数据生成列表[xml]
    *  1.1、支持模糊搜索
    *  1.2、支持插件分页。
    *  1.3、使用 XML 配置 SQL。
    */
    List<Question> queryList(Map map);

    /*
        2. 保存题目 addQuestion [ exam_question ]
         a.入参(1): Question
         b.返回: int
         c.SQL配置方式: 注解
         d.要获取数据库 [自增 ID]
     */
    String FIELDS = "questionTitle,questionAnswer,score,typeId,status,creatorId,createDate";
    String VALUES = "#{questionTitle},#{questionAnswer},#{score},#{typeId},#{status},#{creatorId},now()";
    @Insert("INSERT INTO exam_question("+ FIELDS +") VALUES("+ VALUES +")")
    @Options(keyProperty="id",keyColumn="id",useGeneratedKeys=true)
    int addQuestion(Question question);



    /*
        3. 保存 [选择题选项] [exam_question_options]
          a.入参(1): QuestionOptions
          b.返回: int
          c.方法名: addQuestionOptions
          d.SQL 配置方式: 注解
      */
    String OPS_FIELDS = "questionId,optionA,optionB,optionC,optionD";
    String OPS_VALUES = "#{questionId},#{optionA},#{optionB},#{optionC},#{optionD}";
    @Insert("INSERT INTO exam_question_options("+ OPS_FIELDS +") VALUES("+ OPS_VALUES +")")
    @Options(keyProperty="id",keyColumn="id",useGeneratedKeys=true)
    int addQuestionOptions(QuestionOptions options);



    /*
        4. 根据题目 ID, 查询题目与其相关选项信息。
           分两次 ==> 查询两张表, 先得到 Question 对象.
           再查询第 2 张表得到选项, 再将选项封装到 Question 对象.
          a. 方法名: queryQuestionByQid
          b. 入参: qId
          c. 做结果集封装处理 (在这里发起另一条查询)。
          d. SQL 配置方式: 注解
      */
    @Select("select qe.* from exam_question qe where qe.id=#{id}")
    @Results(
            value = {
                    @Result(column = "id",property = "id",id = true),
                    @Result(column = "id",property = "questionOptions",
                        one = @One(select = "getQuestionOptionsByQID"))
            }
    )
    Question queryQuestionById(Integer qId);

    @Select("select op.* from exam_question_options op " +
            " where op.questionId=#{qId}")
    QuestionOptions getQuestionOptionsByQID(Integer qId);


    /*
        5、更新题库表 [exam_question]
          a.入参(1): Question
          b.返回: int
          c.方法名: updateQuestion
          d.SQL配置方式: 注解
    */
    String UPDATE_FIELDS = "questionTitle=#{questionTitle},questionAnswer=#{questionAnswer},"+
                     "score=#{score},typeId=#{typeId},status=#{status},creatorId=#{creatorId}";
    @Update("update exam_question set "+ UPDATE_FIELDS +" where id=#{id}")
    int updateQuestion(Question question);




    /*
        6、删除选项表记录 [exam_question_options]
           根据问题 id 删除选择题选项表数据
          a.入参(1): Integer qId
          b.返回: int
          c.方法名: deleteQuestionOptions
          d.SQL配置方式: 注解
    */
    @Delete("delete from exam_question_options where questionId=#{qId}")
    int deleteQuestionOptions(Integer qId);


	/*  7.删除题目记录  */
    @Delete("delete from exam_question where id=#{id}")
    int deleteQuestionById(Integer id);

}
